﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls; 
using Microsoft.JCI.Common.Utility;
using Microsoft.JCI.Bll;
using Microsoft.JCI.Common.Entity;
using System.Collections.Generic;

namespace Microsoft.JCI.Portal.UserControl
{
    public partial class DispatchByAgent : System.Web.UI.UserControl
    {
        public string ProvinceCode
        {
            get { return ViewState["ProvinceCode"].ToString(); }
            set { ViewState["ProvinceCode"] = value; }
        }

        public int? CompanyType
        {
            get 
            {
                if (rblSendTo.SelectedValue == "") return null;
                return Convert.ToInt32(rblSendTo.SelectedValue);
            }
        }

        public string BranchCode
        {
            get {
                return ddlBranch.SelectedValue;
            }
        }

        public string VendorCode
        {
            get
            {
                return ddlVendor.SelectedValue;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rblSendTo.Items.Clear();

                EnumeratorHelper[] enums = EnumeratorHelper.GetFieldTexts(typeof(Enumerator.CompanyType));
                foreach (EnumeratorHelper item in enums)
                {
                    if (item.EnumValue == (int)(Enumerator.CompanyType.CallCenter)) continue;

                    rblSendTo.Items.Add(new ListItem(item.EnumDisplayText, item.EnumValue.ToString()));
                }

                if (rblSendTo.Items.Count > 0) rblSendTo.SelectedValue = ((int)Enumerator.CompanyType.Branch).ToString();

                LoadBranch();
            }

        }

        private void LoadBranch()
        { 
            MasterDataBll bll = new MasterDataBll();
            List<SysCompanyEntity> list = bll.GetCompanyListFromCacheByProvince(ProvinceCode);
            ddlBranch.DataSource = list;
            ddlBranch.DataTextField = "Name";
            ddlBranch.DataValueField = "CompanyCode";
            ddlBranch.DataBind();
        }

        private void LoadVendor()
        {
            if (ddlBranch.SelectedValue == "") return;
            MasterDataBll bll = new MasterDataBll();
            List<SysCompanyEntity> list = bll.GetCompanyListFromCacheByParentCode(ddlBranch.SelectedValue);
            ddlVendor.DataSource = list;
            ddlVendor.DataTextField = "Name";
            ddlVendor.DataValueField = "CompanyCode";
            ddlVendor.DataBind();
        }

        protected void rblSendTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadVendor();
        }
    }
}